package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_matter_attach;
import com.power.sifa.approve.model.pageInfo.Apl_matter_attachPageInfo;
import com.power.sifa.approve.api.IApl_matter_attachFeign;
import com.power.sifa.approve.service.Apl_matter_attachService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   申请材料控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_matter_attachController",tags={"申请材料"})
public class Apl_matter_attachController implements IApl_matter_attachFeign{

    @Resource
    private Apl_matter_attachService apl_matter_attachService;


    @Override
    @ApiOperation("查询申请材料分页列表")
    @SaCheckPermission("apl_matter_attach_list")
    public ResponseDTO list(Apl_matter_attachPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_matter_attach> page = this.apl_matter_attachService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_matter_attach apl_matter_attach : page) {
                apl_matter_attach.geneText();
                list.add(apl_matter_attach);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看申请材料")
    @SaCheckPermission("apl_matter_attach_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_matter_attach apl_matter_attach  = apl_matter_attachService.getById(id);
        if (apl_matter_attach != null) {
            apl_matter_attach.geneText();
        }
        return ResponseDTO.succData(apl_matter_attach);
    }

    @Override
    @ApiOperation("删除申请材料")
    @SaCheckPermission("apl_matter_attach_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_matter_attachService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改申请材料")
    @SaCheckPermission("apl_matter_attach_save")
    public ResponseDTO save(Apl_matter_attach apl_matter_attach) {
        Assert.notNull(apl_matter_attach, "apl_matter_attach不能为空");
        apl_matter_attachService.saveOrUpdate(apl_matter_attach);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除申请材料")
    @SaCheckPermission("apl_matter_attach_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_matter_attachService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_matter_attach_list")
    public void output(HttpServletResponse response, Apl_matter_attachPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "申请材料";
        String[] heads = {"applyid+6位顺序号","引用主表id","顺序号","材料名称","是否必传","最大上传数量","描述说明","是否生成下载文件","文件生成方式","引用标准附件id","模板文件地址","单个附件最大大小（单位：m）","是否自定义材料","删除标识（0：有效，1：删除）"};
        String[] fields = {"id","applyid","sortnum","name","require","maxnum","describe","filedown","filemake","stdattachid","tplfile","maxfilesize","ismanual","deleted"};
        Page<Apl_matter_attach> page = this.apl_matter_attachService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "申请材料";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+6位顺序号","引用主表id","顺序号","材料名称","是否必传","最大上传数量","描述说明","是否生成下载文件","文件生成方式","引用标准附件id","模板文件地址","单个附件最大大小（单位：m）","是否自定义材料","删除标识（0：有效，1：删除）"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","sortnum","name","require","maxnum","describe","filedown","filemake","stdattachid","tplfile","maxfilesize","ismanual","deleted"};
        List<Apl_matter_attach> list = (List<Apl_matter_attach>)ExcelUtil.Import(fields, file, Apl_matter_attach.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_matter_attachService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    @ApiOperation(value="初始化申请材料")
    public ResponseDTO init(String applyid, String matid) {
        Assert.isTrue(StringUtils.hasText(applyid), "申请id不能为空");
        Assert.isTrue(StringUtils.hasText(matid), "事项id不能为空");
        return ResponseDTO.succData(apl_matter_attachService.init(applyid, matid));
    }

    @Override
    @ApiOperation(value="根据申请id获取申请材料列表及附件")
    public ResponseDTO queryListByApplyid(String applyid) {
        Assert.isTrue(StringUtils.hasText(applyid), "申请id不能为空");
        return ResponseDTO.succData(apl_matter_attachService.queryListAndFileByApplyid(applyid));
    }

    @Override
    @ApiOperation(value="下载所有材料")
    public void downloadMaterialAll(String applyid, HttpServletResponse response) throws Exception {
        Assert.isTrue(StringUtils.hasText(applyid), "申请id不能为空");
        apl_matter_attachService.downloadMaterialAll(applyid, response);
    }


    @Override
    public ResponseDTO queryThirdPersonMaterial(String applyid) {
        return ResponseDTO.succData(this.apl_matter_attachService.queryThirdPersonMaterial(applyid));
    }

}
